﻿using UnityEngine;
using UnityEditor;

namespace Yoozoo.UI.YGUI
{
    //拷贝/扩展自YRichTextEditor
    [CustomEditor(typeof(YRichUnderlineText), true)]
    [CanEditMultipleObjects]
    public class YRichUnderlineTextEditor : YTextEditor
    {
        YRichUnderlineText _yRichText;
        SerializedProperty m_InputText;
        SerializedProperty m_Dirty;
        SerializedProperty m_LineAlign;
        SerializedProperty m_OnClickProperty;
        SerializedProperty m_UnderlineHeight;
        FontData2Editor drawer;

        protected override void OnEnable()
        {
            base.OnEnable();
            _yRichText = target as YRichUnderlineText;
            m_InputText = serializedObject.FindProperty("m_RichText");
            m_Dirty = serializedObject.FindProperty("m_Dirty");
            m_LineAlign = serializedObject.FindProperty("m_LineAlignment");
            m_OnClickProperty = serializedObject.FindProperty("m_OnClick");
            m_UnderlineHeight = serializedObject.FindProperty("m_UnderlineHeight");
            drawer = new FontData2Editor(m_LineAlign);
            _yRichText.alignByGeometry = true;
        }

        public override void DrawTextArea()
        {
            EditorGUILayout.PropertyField(m_InputText, new GUIContent("InputText"));
            EditorGUI.BeginDisabledGroup(true);
            EditorGUILayout.PropertyField(m_Text, new GUIContent("OutText"));
            EditorGUI.EndDisabledGroup();
        }

        public override void DrawFont()
        {
            float h = drawer.GetPropertyHeight(m_FontData, new GUIContent(""));
            Rect rect = EditorGUILayout.GetControlRect(false, h);
            drawer.OnGUI(rect, m_FontData, new GUIContent(""));
        }

        public override void OnInspectorGUI()
        {
            float height = EditorGUILayout.FloatField(new GUIContent("Underline Height"),_yRichText.m_UnderlineHeight);
            if(height != _yRichText.m_UnderlineHeight)
            {
                _yRichText.m_UnderlineHeight = height;
                _yRichText.SetVerticesDirty();
            }
            base.OnInspectorGUI();
            if (m_Dirty.boolValue)
            {
                _yRichText.DrawTag();
            }
            if (!_yRichText.alignByGeometry)
            {
                _yRichText.alignByGeometry = true;
            }
            if (!_yRichText.supportRichText)
            {
                _yRichText.supportRichText = true;
            }
            serializedObject.Update();
            EditorGUILayout.PropertyField(m_OnClickProperty);
            serializedObject.ApplyModifiedProperties();
        }
    }
}